//
//  VideoPlayViewController.m
//  BosidengIphone
//
//  Created by  on 12-1-4.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "VideoPlayViewController.h"

@implementation VideoPlayViewController
@synthesize mPlayer;

- (id)initWithContentURL:(NSURL *)url
{
    self = [super init];
    if (self) {
        MPMoviePlayerController *player = [[MPMoviePlayerController alloc] init];
        player.contentURL = url;
        self.mPlayer = player;
        [player release];
        
        [[NSNotificationCenter defaultCenter] addObserver:self 
												 selector:@selector(moviePlayerLoadStateChanged:)
													 name:MPMoviePlayerLoadStateDidChangeNotification 
												   object:nil];
		[[NSNotificationCenter defaultCenter] addObserver:self 
												 selector:@selector(moviePlayerDidFinish:)
													 name:MPMoviePlayerPlaybackDidFinishNotification
												   object:nil];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self
													name:MPMoviePlayerLoadStateDidChangeNotification 
												  object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self
													name:MPMoviePlayerPlaybackDidFinishNotification 
												  object:nil];
    [mPlayer release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (BOOL)isStopped {
	if (mPlayer) {
		MPMoviePlaybackState state = mPlayer.playbackState;
		return (state == MPMoviePlaybackStateStopped);
	}
	return YES;
}

- (void)play {
	if (mPlayer) {
		MPMoviePlaybackState state = mPlayer.playbackState;
		if (state != MPMoviePlaybackStatePlaying) {
			[mPlayer play];
		}
	}
	[waitView startAnimating];
	isNeedStop = NO;
}

- (void)stop {
	if (mPlayer) {
		MPMoviePlaybackState state = mPlayer.playbackState;
		if (state == MPMoviePlaybackStatePlaying) {
			[mPlayer stop];
		}
		//[mPlayer release];
		//mPlayer = nil;
	}
	[waitView stopAnimating];
	isNeedStop = YES;
}

- (void)viewDidLoad
{
    //[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
    [super viewDidLoad];
    
    CGRect frame = self.view.bounds;
    [mPlayer.view setFrame:frame];
    [self.view addSubview: mPlayer.view];
    
    waitView = [[UIActivityIndicatorView alloc] 
                initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    waitView.hidesWhenStopped = YES;
    int w = 20;
    int h = 20;
    int x = (frame.size.width - w) / 2;
    int y = (frame.size.height - h) / 2;
    waitView.frame = CGRectMake(x, y, w, h);
    [self.view addSubview:waitView];
    [waitView release];
    
    [self play];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [self stop];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

- (void)moviePlayerDidFinish:(NSNotification *)notification {
	//NSLog(@"NSNotification moviePlayerDidFinish");
	[waitView stopAnimating];
}

- (void)moviePlayerLoadStateChanged:(NSNotification *)notification {
	MPMovieLoadState state = [mPlayer loadState];
	if (state == MPMovieLoadStatePlayable) {
		//NSLog(@"NSNotification MPMovieLoadStatePlayable");
		[waitView stopAnimating];
	}
	if (state == MPMovieLoadStatePlaythroughOK) {
		//NSLog(@"NSNotification MPMovieLoadStatePlaythroughOK");
		[waitView stopAnimating];
	}
	if (state == MPMovieLoadStateStalled) {
		//NSLog(@"NSNotification MPMovieLoadStateStalled");
		[waitView startAnimating];
	}
	if (state == MPMovieLoadStateUnknown) {
		//NSLog(@"NSNotification MPMovieLoadStateUnknown");
		[waitView startAnimating];
	}
	if (isNeedStop) {
		[self stop];
	}
}

@end
